package com.zxl.manager.privacy.utils.track.crash;

import android.content.Context;

import com.zxl.manager.privacy.utils.track.MobclickAgentHelper;
import com.zxl.manager.privacy.utils.utils.LogUtils;

/**
 * 
 * 类名称：CrashReportManager
 * 类描述：错误信息收集管理类
 * 创建人：makai
 * 修改人：makai
 * 修改时间：2014年6月4日 下午9:37:47
 * 修改备注：
 * @version 1.0.0
 *
 */
public class CrashReportManager {

	public Context context;
	
	public String logPath;
	
	public String svn;
	
	public String channel;
	
	public String customData;
	
	private CrashReport mCrashReport;
	
	private static CrashReportManager sInstance;
	
	private CrashReportManager() {
	}
	
	public static CrashReportManager getInstance() {
		if (null == sInstance) {
			sInstance = new CrashReportManager();
		}
		return sInstance;
	}
	
	/**
	 * 
	 * init初始化资源
	 *必须在调用{@link start()}之前使用
	 * @param context
	 * @param logPath	错误日志保存路径
	 * @param svn		当前的svn版本号
	 * @param channel	渠道号
	 * @return 
	 *CrashReportManager
	 * @exception 
	 * @since  1.0.0
	 */
	public CrashReportManager init(Context context, String logPath, String svn, String channel) {
		this.context = context.getApplicationContext();
		this.logPath = logPath;
		this.svn = svn;
		this.channel = channel;
		LogUtils.d("CrashReportManager init " + channel);
		return this;
	}
	
	/**
	 * 
	 * start开始收集错误日志
	 * 真正建立错误日志监听
	 * @return 
	 *CrashReportManager
	 * @exception 
	 * @since  1.0.0
	 */
	public CrashReportManager start() {
		if (null == context) {
			throw new RuntimeException("not call init method !!!");
		}
		if (null == mCrashReport) {
			mCrashReport = new CrashReport(context, logPath, channel);
		}
		return this;
	}
	
	public void exception(String message, Throwable ex) {
		MobclickAgentHelper.reportError(context, mCrashReport.exception(message, ex).toString());
	}
	
	/**
	 * 
	 * setCustomData添加自定义错误信息
	 * 可以提供调用者上传一些自定义的信息
	 * @param data 	错误信息
	 *void
	 * @exception 
	 * @since  1.0.0
	 */
	public void setCustomData(String data) {
		customData = data;
	}
	
	/**
	 * 
	 * stop停止错误信息收集
	 * 回收资源
	 *void
	 * @exception 
	 * @since  1.0.0
	 */
	public void stop() {
		mCrashReport = null;
	}
	
}
